/**
 * main JS
 * @authors zhuhui (zhuhui@yidian-mall.com)
 * @date    2019-03-28 15:01:05
 * @version $Id$
 */
$(function() {
    objects.MuneBtn('#menu-btn');
    objects.MenuClick('.nav-item');
    objects.MenuScoller('.menu-list');
    objects.ListScoller('#list');
    objects.ListScoller('#right-side');
    objects.SwiperSlider('.swiper-free');
    $('#list-more').bind('click',objects.ListMore);
    $(window).resize(function() {
        objects.MenuScoller('.menu-list');
        objects.ListScoller('#list');
        objects.ListScoller('#right-side');
    });
});

var objects = {
    MuneBtn:function(ele) {
        $(ele).click(function(e) {
            var _header = $('#header'),cn = 'header-show';
            if(!_header.hasClass(cn)){
                _header.addClass(cn);
            }else{
                _header.removeClass(cn);
            }
        });
    },
    MenuClick:function(ele) {
        $(ele).click(function(e) {
            window.event? window.event.cancelBubble = true : e.stopPropagation();
            var _class = {active:'sub-down active',left:'sub-left',down:'sub-down',flag:'.nav-item'},
                _this = $(this),
                _siblings = _this.siblings(_class.flag);
            if(_this.hasClass(_class.left)){
                _this.removeClass(_class.left).addClass(_class.active);
                objects.MenuHide(_class,_siblings);
            }else if(_this.hasClass(_class.down)){
                objects.MenuHide(_class,_this);
            }else{
                objects.MenuHide(_class,_siblings);
            };
        });
    },
    MenuHide:function(_class,eles) {
        for (var i = 0; i < eles.length; i++) {
            if ($(eles[i]).hasClass(_class.down)) {
                    $(eles[i]).removeClass(_class.active).addClass(_class.left);
                    $(eles[i]).find(_class.flag).each(function(index, el) {
                        if ($(el).hasClass(_class.down)) {
                            $(el).removeClass(_class.active).addClass(_class.left);
                        }
                    });
            }
        }
    },
    MenuScoller:function(ele) {
        if ($(window).outerWidth(true) <= 768) {
            $(ele).mCustomScrollbar('destroy');
            $(ele).mCustomScrollbar({
                axis: 'y',
                theme:'minimal',
                scrollInertia:0,// 取消平滑滚动
            });
        }else{
            objects.ScollerSimpleOperate(ele,'destroy');
        }
    },
    ScollerSimpleOperate:function(ele,type) {
        if (typeof(type) != 'undefined' && (type == 'destroy' || type == 'update')) {
            $(ele).mCustomScrollbar(type);
            if (type == 'destroy') {
                $(ele).removeAttr('style').removeClass('mCS_destroyed');
            }
        }
    },
    ListScoller:function(ele,type) {
        if (typeof(type) != 'undefined' && (type == 'destroy' || type == 'update')) {
            objects.ScollerSimpleOperate(ele,type);
        }else{
            $(ele).mCustomScrollbar({
                axis: 'y',
                theme:'minimal',
                scrollInertia:0,// 取消平滑滚动
                callbacks:{
                    onTotalScroll:function(){
                        objects.ListMore();
                    }
                }
            });
        }
    },
    SwiperSlider:function(_class) {
        var swiper = new Swiper(_class, {
            pagination: '.pagination-free',
            paginationClickable: true,
            spaceBetween: 30,
            centeredSlides: true,
            autoplay: 2500,
            autoplayDisableOnInteraction: false,
            loop:true,
        });
    },
    ListMore:function() {
        var _this = $('#list-more'),
            offset = _this.attr('data-offset'),
            limit = _this.attr('data-limit'),
            count = _this.attr('data-count'),
            url_path = _this.attr('data-url'),
            next_offset = parseInt(offset)+parseInt(limit),
            txt = '加载中...';
        _this.find('span').html(txt);
        if (count > next_offset) {
            $.ajax({
                url: url_path,
                type: 'POST',
                dataType: 'html',
                data: {offset: offset,limit:limit},
            })
            .done(function(data) {
                if (data != '') {
                    $('#list .mCSB_container').append(data);
                    objects.ListScoller($('#list'),'update');
                    _this.attr('data-offset', next_offset);
                    txt = '更多';
                }else{
                    txt = '已经到底了';
                }
                _this.find('span').html(txt);
            })
            .fail(function() {
                _this.find('span').html('数据发生错误,请稍后再试');
            });
        }else{
            _this.find('span').html(txt);
        }
    }

};
